package cn.iocoder.yudao.module.game.service.withdrawalAccount;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.game.controller.admin.withdrawalAccount.vo.*;
import cn.iocoder.yudao.module.game.dal.dataobject.withdrawalAccount.WithdrawalAccountDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.game.dal.mysql.withdrawalAccount.WithdrawalAccountMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.game.enums.ErrorCodeConstants.*;

/**
 * 提款账号 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class WithdrawalAccountServiceImpl implements WithdrawalAccountService {

    @Resource
    private WithdrawalAccountMapper withdrawalAccountMapper;

    @Override
    public Long createWithdrawalAccount(WithdrawalAccountSaveReqVO createReqVO) {
        // 插入
        WithdrawalAccountDO withdrawalAccount = BeanUtils.toBean(createReqVO, WithdrawalAccountDO.class);
        withdrawalAccountMapper.insert(withdrawalAccount);
        // 返回
        return withdrawalAccount.getId();
    }

    @Override
    public void updateWithdrawalAccount(WithdrawalAccountSaveReqVO updateReqVO) {
        // 校验存在
        validateWithdrawalAccountExists(updateReqVO.getId());
        // 更新
        WithdrawalAccountDO updateObj = BeanUtils.toBean(updateReqVO, WithdrawalAccountDO.class);
        withdrawalAccountMapper.updateById(updateObj);
    }

    @Override
    public void deleteWithdrawalAccount(Long id) {
        // 校验存在
        validateWithdrawalAccountExists(id);
        // 删除
        withdrawalAccountMapper.deleteById(id);
    }

    private void validateWithdrawalAccountExists(Long id) {
        if (withdrawalAccountMapper.selectById(id) == null) {
            throw exception(WITHDRAWAL_ACCOUNT_NOT_EXISTS);
        }
    }

    @Override
    public WithdrawalAccountDO getWithdrawalAccount(Long id) {
        return withdrawalAccountMapper.selectById(id);
    }

    @Override
    public PageResult<WithdrawalAccountDO> getWithdrawalAccountPage(WithdrawalAccountPageReqVO pageReqVO) {
        return withdrawalAccountMapper.selectPage(pageReqVO);
    }

}